package com.solin.blog.web.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author solin
 * @description registrationBean 配置器
 * 通过 registrationBean 的方式初始化 servlet filter listener
 * @since 2022/2/12 15:26
 */
@Configuration
public class MyRegistConfig {
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        TestServlet testFilter=new TestServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(testFilter, "/servlet");
        return servletRegistrationBean ;
    }

    @Bean
    public FilterRegistrationBean getFilter(){
       TestFilter testFilter=new TestFilter();
       FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(testFilter);
       filterRegistrationBean.setUrlPatterns(Arrays.asList("/servlet"));
       return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean  getListener(){
        TestListener testListener=new TestListener();
        ServletListenerRegistrationBean  servletListenerRegistrationBean=new ServletListenerRegistrationBean(testListener);
        return servletListenerRegistrationBean;
    }
}
